package com.lemon.cloud.mp.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.comm.constants.enums.LogBusType;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.log.annotation.SysLog;
import com.lemon.cloud.mp.entity.WxAccountFans;
import com.lemon.cloud.mp.service.WxAccountFansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wxaccountfans")
public class WxAccountFansController {

    @Autowired
    private WxAccountFansService wxAccountFansService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param wxAccountFans 微信公众号粉丝
     * @return
     */
    @GetMapping("/page")
    public ResultMsg<IPage<WxAccountFans>> getWxAccountFansPage(Page<WxAccountFans> page, WxAccountFans wxAccountFans) {
        return ResultMsg.resultSuccess(wxAccountFansService.page(page, Wrappers.query(wxAccountFans)));
    }

    /**
     * 通过id查询微信公众号粉丝
     *
     * @param id id
     * @return ResultMsg
     */
    @GetMapping("/{id}")
    public ResultMsg<WxAccountFans> getById(@PathVariable("id") Integer id) {
        return ResultMsg.resultSuccess(wxAccountFansService.getById(id));
    }

    /**
     * 新增微信公众号粉丝
     *
     * @param wxAccountFans 微信公众号粉丝
     * @return ResultMsg
     */
    @SysLog(value = "新增微信公众号粉丝", businessType = LogBusType.ADD)
    @PostMapping
    @PreAuthorize("hasAuthority('mp_wxaccountfans_add')")
    public ResultMsg<Boolean> save(@RequestBody WxAccountFans wxAccountFans) {
        return ResultMsg.resultSuccess(wxAccountFansService.save(wxAccountFans));
    }

    /**
     * 修改微信公众号粉丝
     *
     * @param wxAccountFans 微信公众号粉丝
     * @return ResultMsg
     */
    @SysLog("修改微信公众号粉丝")
    @PutMapping
    @PreAuthorize("hasAuthority('mp_wxaccountfans_edit')")
    public ResultMsg<Boolean> updateById(@RequestBody WxAccountFans wxAccountFans) {
        return ResultMsg.resultSuccess(wxAccountFansService.updateById(wxAccountFans));
    }

    /**
     * 通过id删除微信公众号粉丝
     *
     * @param id id
     * @return ResultMsg
     */
    @SysLog("删除微信公众号粉丝")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('mp_wxaccountfans_del')")
    public ResultMsg<Boolean> removeById(@PathVariable("id") Integer id) {
        return ResultMsg.resultSuccess(wxAccountFansService.removeById(id));
    }

    /**
     * 同步粉丝
     *
     * @param appId
     * @return
     */
    @PostMapping("/sync/{appId}")
    @PreAuthorize("hasAuthority('mp_wxaccountfans_sync')")
    public ResultMsg<Boolean> sync(@PathVariable("appId") String appId) {
        return ResultMsg.resultSuccess(wxAccountFansService.syncAccountFans(appId));
    }

}
